<template>
  <div>
    <el-card style="margin: 20px 0px;">
      <CategorySelect @getcategoryId="getcategoryId" :show="scene == 0"></CategorySelect>
    </el-card>
    <el-card>
      <!-- spu列表 -->
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" :disabled="!category3Id" @click="addSpu">添加SPU</el-button>

        <el-table style="width: 100%" border :data="records">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>

          <el-table-column prop="spuName" label="spu名称" width="width">
          </el-table-column>

          <el-table-column prop="description" label="spu描述" width="width">
          </el-table-column>

          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{row,$index}">
              <!-- 自定义一个封装组，来展示title -->
              <hint-button type="success" icon="el-icon-plus" size="mini" title="添加sku"
                @click="reqAddSku(row)"></hint-button>

              <el-button type="warning" icon="el-icon-edit" size="mini" title="修改spu"
                @click="updateSpu(row)"></el-button>

              <el-button type="info" icon="el-icon-info" size="mini" title="查看当前spu全部sku列表"
                @click="handler(row)"></el-button>

              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="deleteSpu(row)">
                <el-Button type="danger" icon="el-icon-delete" size="mini" title="删除spu" slot="reference"
                  style="margin-left: 10px;"></el-Button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <el-dialog :title="`${spu.spuName}的sku列表`" :visible.sync="dialogTableVisible" :before-close="close">
          <el-table :data="skuList" v-loading="loading">
            <el-table-column prop="skuName" label="名称" width="150"></el-table-column>
            <el-table-column prop="price" label="价格" width="200"></el-table-column>
            <el-table-column prop="weight" label="重量"></el-table-column>
            <el-table-column prop="prop" label="默认图片" width="150">
              <template slot-scope="{row,$index}">
                <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px">
              </template>
            </el-table-column>

          </el-table>
        </el-dialog>

        <!--   -->
        <el-pagination :current-page="page" style="text-align: center;" :page-sizes="[3, 5, 10]" :page-size="limit"
          @current-change="getSpuList" @size-change="handleCurrentChange"
          layout=" prev, pager, next, jumper,->,sizes,total" :total="total">
        </el-pagination>
      </div>

      <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spu"></SpuForm>
      <SkuForm v-show="scene == 2" @changeScene="changeScene" ref="sku"></SkuForm>

    </el-card>
  </div>
</template>

<script>
import SkuForm from './SkuForm'
import SpuForm from './SpuForm'

export default {
  name: "Spu",
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',

      show: true,
      page: 1,
      limit: 3,
      records: [],
      total: 0,
      scene: 0,
      dialogTableVisible: false,
      spu: {},
      skuList: [],
      loading: true
    }
  },
  methods: {
    //对话框关闭之前的回调
    close(done) {
      this.loading = true;
      this.skuList = [];
      //关闭对话框
      done();
    },
    //查看当前spu的sku列表
    async handler(row) {
      this.dialogTableVisible = true;
      // console.log(row);
      this.spu = row

      let result = await this.$API.spu.reqSkuList(this.spu.id)
      if (result.code == 200) {
        this.skuList = result.data;
        this.loading = false
      }
    },
    reqAddSku(row) {
      this.scene = 2
      this.$refs.sku.getData(this.category1Id, this.category2Id, row)
    },
    async deleteSpu(row) {
      let result = await this.$API.spu.reqDeleteSpu(row.id)
      if (result.code == 200) {
        this.$message({ type: "success", message: "删除成功" });
        let page = this.records.length > 1 ? this.page : this.page - 1;
        if (page <= 0) page = 1
        this.getSpuList(page)
      }
    },
    async getSpuList(pager = 1) {
      this.page = pager;
      const { page, limit, category3Id } = this;
      let result = await this.$API.spu.reqSpuList(page, limit, category3Id);
      if (result.code == 200) {
        // console.log(result);
        this.total = result.data.total;
        this.records = result.data.records;
      }
    },
    getcategoryId({ categoryId, level }) {
      if (level == 1) {
        this.category1Id = categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (level == 2) {
        this.category2Id = categoryId
        this.category3Id = ''
      } else {
        this.category3Id = categoryId
        this.getSpuList()
      }
    },
    handleCurrentChange(limit) {
      this.limit = limit;
      this.getSpuList()
    },
    addSpu() {
      this.scene = 1;
      this.$refs.spu.addSpuData(this.category3Id);
    },
    updateSpu(row) {
      this.scene = 1;
      this.$refs.spu.initSpuData(row)//ref用于获取想要的组件，这样可以调用组件的所有东西
      // console.log(row);

    },
    changeScene({ scene, flag }) {
      this.scene = scene
      if (flag === '修改')
        this.getSpuList(this.page)
      else {
        this.getSpuList(1)
      }
    }

  },
  components: {
    SkuForm,
    SpuForm
  },

}
</script>


<style scoped></style>